<template>
    <div class="app-container">
        <el-row>
            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="渠道号" prop="channelId">
                    <el-input
                        v-model="queryParams.channelId" placeholder="请输入渠道号" clearable style="width: 180px"
                        @keyup.enter="handleSearch" />
                </el-form-item>
                <el-form-item label="渠道名称" prop="channelName">
                    <el-select
                        v-model="queryParams.channelName"
                        clearable
                        placeholder="请输入选择渠道"
                        @keyup.enter="handleSearch"
                        style="width: 180px"
                    >
                        <el-option
                            v-for="item in getChannel"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleSearch">搜索</el-button>
                    <el-button icon="Refresh" @click="resetSearch">重置</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="Plus" plain @click="handleCreate" v-permission="['sys.plat.channel.area']">新增
                </el-button>
            </el-col>
        </el-row>
        <el-table v-loading="tableLoading" :data="tableData" row-key="id" stripe >
            <template v-for="(item, idx) in columns" :key="idx">
                <el-table-column :prop="item.prop" :label="item.label" :fixed="item.fixed" v-if="item.show"
                                 :show-overflow-tooltip="true" :align="item.align" :width="item.width" :sortable="item.sortable">
                    <template v-if="item.prop == 'createdAt'" #default="scope">
                        <span>{{ formatTime(scope.row.createdAt) }}</span>
                    </template>
                    <template v-if="item.prop == 'channelName'" #default="scope">
                        <span>{{ getChannelTxt(scope.row.channelName) }}</span>
                    </template>
                    <template v-if="item.prop == 'link'" #default="scope">
                        <el-link :href="scope.row.link" type="success" target="_blank">{{ scope.row.link }}</el-link>
                    </template>
                    <template v-if="item.prop == 'disabled'" #default="scope">
                        <el-switch
                            v-model="scope.row.disabled"
                            :active-value="false"
                            :inactive-value="true"
                            active-color="green"
                            inactive-color="red"
                            @change="disabledChange(scope.row)"
                        />
                    </template>
                </el-table-column>

            </template>
            <el-table-column fixed="right" header-align="center" align="center" label="操作" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-tooltip content="修改" placement="top">
                        <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                                   v-permission="['sys.config.dict.update']"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top" v-if="scope.row.path != '0001'">
                        <el-button link type="danger" icon="Delete"
                                   @click="handleDelete(scope.row)" v-permission="['sys.config.dict.delete']"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <el-row>
            <pagination :total="queryParams.totalCount" v-model:page="queryParams.pageNo"
                        v-model:limit="queryParams.pageSize" @pagination="list" />
        </el-row>
        <el-dialog title="新增渠道" v-model="showCreate" width="40%">
            <el-form ref="createRef" :model="formData" :rules="formRules" label-width="120px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="选择渠道" prop="channelId">
                            <el-input v-model="formData.channelId" placeholder="请输入渠道ID" maxlength="100" type="text" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="渠道" prop="channelName">
                            <el-select
                                v-model="formData.channelName"
                                clearable
                                placeholder="请输入选择渠道"
                            >
                                <el-option
                                    v-for="item in getChannel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="渠道链接" prop="link">
                            <el-input v-model="formData.link" placeholder="请输入渠道链接" maxlength="256"
                                      auto-complete="off" type="text" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="disabled" label="渠道状态">
                            <el-switch
                                v-model="formData.disabled"
                                :active-value="false"
                                :inactive-value="true"
                                active-color="green"
                                inactive-color="red"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="create">确 定</el-button>
                    <el-button @click="showCreate = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog title="修改渠道" v-model="showUpdate" width="40%">
            <el-form ref="updateRef" :model="formData" :rules="formRules" label-width="120px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="渠道号" prop="channelId">
                            <el-input v-model="formData.channelId" placeholder="请输入渠道ID" maxlength="100" type="text" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="渠道" prop="channelName">
                            <el-select
                                v-model="formData.channelName"
                                clearable
                                placeholder="请输入选择渠道"
                            >
                                <el-option
                                    v-for="item in getChannel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="渠道链接" prop="link">
                            <el-input v-model="formData.link" placeholder="请输入渠道链接" maxlength="256"
                                      auto-complete="off" type="text" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item prop="disabled" label="渠道状态">
                            <el-switch
                                v-model="formData.disabled"
                                :active-value="false"
                                :inactive-value="true"
                                active-color="green"
                                inactive-color="red"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="update">确 定</el-button>
                    <el-button @click="showUpdate = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>
<script setup lang="ts" name="platform-sys-plat">
import { nextTick, onMounted, reactive, ref } from 'vue'
import modal from '/@/utils/modal'
import { getList,doCreate,doDisable,doUpdate,doDelete,getInfo } from '/@/api/platform/plat/channel'
import { toRefs } from '@vueuse/core'
import { ElForm } from 'element-plus'
import { formatTime } from '/@/utils/common'

import {getChannel,getChannelTxt} from "/@/utils/channel";

const createRef = ref<InstanceType<typeof ElForm>>()
const updateRef = ref<InstanceType<typeof ElForm>>()
const queryRef = ref<InstanceType<typeof ElForm>>()

const tableData = ref([])
const tableLoading = ref(false)
const showSearch = ref(true)
const showCreate = ref(false)
const showUpdate = ref(false)

const data = reactive({
    formData: {},
    queryParams: {
        pageNo: 1,
        pageSize: 10,
        totalCount: 0,
        pageOrderName: 'createdAt',
        pageOrderBy: 'descending'
    },
    formRules: {
        channelId: [{ required: true, message: "渠道ID不能为空", trigger: "blur" }],
        channelName: [{ required: true, message: "渠道名称不能为空", trigger: "blur" }],
        link: [{ required: true, message: "渠道链接不能为空", trigger: "blur" }],
    },
})

const { formData, queryParams,formRules } = toRefs(data)


const columns = ref([
    { prop: 'id', label: `ID`, show: true, fixed: 'left', width: 200 },
    { prop: 'channelId', label: `渠道号`, show: true, fixed: 'left' },
    { prop: 'channelName', label: `渠道名称`, show: true, fixed: 'left' },
    { prop: 'link', label: `渠道链接`, show: true, fixed: 'left' },
    { prop: 'disabled', label: `状态`, show: true, fixed: false },
    { prop: 'createdAt', label: `创建时间`, show: true, fixed: false, width: 160 }
])
const handleSearch = () => {
    queryParams.value.pageNo = 1
    list()
}
const resetSearch = () => {
    queryRef.value?.resetFields()
    list()
}
// 查询表格
const list = () => {
    tableLoading.value = true
    getList(queryParams.value).then((res) => {
        tableLoading.value = false
        tableData.value = res.data.list as never
        queryParams.value.totalCount = res.data.totalCount as never
    })
}
// 提交新增
const create = () => {
    if (!createRef.value) return
    createRef.value.validate((valid) => {
        if (valid) {
            doCreate(formData.value).then((res: any) => {
                modal.msgSuccess(res.msg)
                showCreate.value = false
                resetSearch()
            })
        }
    })
}
// 修改按钮
const handleUpdate = (row: any) => {
    getInfo(row.id).then((res: any) => {
        formData.value = res.data
        showUpdate.value = true
    })
}

// 删除按钮
const handleDelete = (row: any) => {
    modal.confirm('此操作将删除 '+row.channelName+' 及其下级，请谨慎操作！').then(() => {
        return doDelete(row.id)
    }).then(() => {
        list()
        modal.msgSuccess('删除成功')
    }).catch(() => { })
}
const resetForm = (formEl: InstanceType<typeof ElForm> | undefined) => {
    formData.value = {
        id: '',
        channelId: '',
        channelName: '',
        link: '',
        disabled: false,
    }
    formEl?.resetFields()
}
const handleCreate = (row: any) => {
    resetForm(createRef.value)
    showCreate.value = true
}
// 启用禁用
const disabledChange = (row: any) => {
    doDisable({disabled: row.disabled, id: row.id}).then((res: any) => {
        modal.msgSuccess(res.msg)
        list()
    }).catch(()=>{
        setTimeout(() => {
            row.disabled = !row.disabled
        }, 300)
    })
}
// 提交修改
const update = () => {
    if (!updateRef.value) return
    updateRef.value.validate((valid) => {
        if (valid) {
            doUpdate(formData.value).then((res: any) => {
                modal.msgSuccess(res.msg)
                showUpdate.value = false
                resetSearch()
            })
        }
    })
}
onMounted(() => {
    list();
})
</script>
<!--定义布局-->
<route lang="yaml">
meta:
 layout: platform/index
</route>
<style scoped>
.time {
    font-size: 11px;
    background-color: rgb(135, 208, 104);
    color: #fff;
    display: inline-block;
    width: 38px;
    border-radius: 2px;
    padding-left: 1px;
    height: 22px;
}
.content {
    white-space:normal;
    word-break:break-all;
}
</style>
